﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace YJKFamilyLibManager
{
	public partial class ProgressBarForm : System.Windows.Forms.Form
	{
		public bool IsStoped
		{
			get
			{
				return this.stopTag;
			}
		}

		public ProgressBarForm(List<KeyValuePair<int, string>> subTypeInfoList, string Caption, bool IsCancel)
		{
			this.InitializeComponent();
			foreach (KeyValuePair<int, string> keyValuePair in subTypeInfoList)
			{
				this.maximum += keyValuePair.Key;
			}
			this.mainProgressBar.Maximum = this.maximum;
			this.m_SubTypeInfoList = subTypeInfoList;
			this.mainProgressBar.Value = 0;
			this.subProgressBar.Maximum = subTypeInfoList[0].Key;
			this.subProgressBar.Value = 0;
		}

		public bool ProgressStep(int step)
		{
			if (this.stopTag)
			{
				base.Dispose();
				return false;
			}
			if (this.counter > this.mainProgressBar.Maximum)
			{
				base.Dispose();
				return false;
			}
			int currentSubIndex = this.GetCurrentSubIndex();
			if (this.currentIndex != currentSubIndex)
			{
				this.currentIndex = currentSubIndex;
				this.subProgressBar.Maximum = this.m_SubTypeInfoList[this.currentIndex].Key;
				this.subProgressBar.Value = 0;
			}
			this.mainProgressBar.Value += step;
			this.mainLabel.Text = "目前总体完成:" + this.mainProgressBar.Value * 100 / this.mainProgressBar.Maximum + "%";
			this.subProgressBar.Value += step;
			string value = this.m_SubTypeInfoList[this.currentIndex].Value;
			this.subLabel.Text = string.Concat(new object[]
			{
				value,
				"部分完成",
				this.subProgressBar.Value * 100 / this.subProgressBar.Maximum,
				"%"
			});
			Application.DoEvents();
			this.counter++;
			return true;
		}

		private int GetCurrentSubIndex()
		{
			int num = 0;
			int num2 = this.counter;
			foreach (KeyValuePair<int, string> keyValuePair in this.m_SubTypeInfoList)
			{
				if (num2 < keyValuePair.Key)
				{
					return num;
				}
				num2 -= keyValuePair.Key;
				num++;
			}
			return num;
		}

		private void ProgressBarForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (MessageBox.Show("你确定中止吗", "中止", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
			{
				this.stopTag = true;
				return;
			}
			this.stopTag = false;
			e.Cancel = true;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private List<KeyValuePair<int, string>> m_SubTypeInfoList = new List<KeyValuePair<int, string>>();

		private int maximum;

		private int counter;

		private int currentIndex;

		private bool stopTag;
	}
}
